<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/17
 * Time: 11:50
 */

namespace app\admin\controller;
use app\common\model\Order_log;
use app\common\traits\Page;
use think\Db;
use app\common\model\Order as Ordera;
use app\admin\model\Orders;
class Order extends Admin
{
    protected $user_model;
    use Page;
    public function _initialize()
    {
        parent::_initialize();
        $this->user_model = new Ordera();
    }

    /**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index()
    {
        $keyword = input('keyword');
        $user_id = input('tjr_id');
        $status = input('status');
        $map = [];
        if ($keyword) {
            $map['name|tel'] = ['like', "%{$keyword}%"];
        }
        if ($user_id) {
            $map['user_id'] = $user_id;
        }
        if($status){
            $map['status'] = $status;
        }
//        $map['daoru'] = 3;
//        $user_list = $this->pageshow('Orders',$map,'','user');
        $user_list = $this->pageshow('Orders',$map);
        $this->assign('status',model('orders')->getStatus() );

        $depar = Db::name('customer')->select();
        return $this->fetch('index', ['user_list' => $user_list, 'keyword' => $keyword,'user'=>$depar,'user_id'=>$user_id]);
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add()
    {
        $depar = Db::name('department')->select();
        $this->assign('depar',$depar);
        return $this->fetch();
    }
    /**
     * 邀约经理审核转介绍订单
     * @return mixed
     */
    public function sh(){
        $data = $this->request->param();
        $khd = isset($data['khd'])?$data['khd']:1;
        $sendhuashu = new \app\common\model\SendHuaShua();
        if($data['act'] == 1){//通过
            $ordersinfo = Db::name('orders')->where('id',$data['id'])->find();//修改中间表
            $employee = Db::name('employee')->where(['phone'=>$ordersinfo->user->mobile])->find();
            $orderdata['daoru'] = 3;
            $orderdata['name'] = $ordersinfo['name'];
            $orderdata['maphone'] = $ordersinfo['tel'];
            $orderdata['ywstate'] = 4;
            $orderdata['status'] = 1;
            $orderdata['qdbz'] = $ordersinfo['bz'];
            $orderdata['qudao'] = $ordersinfo->user->attr;
            $orderdata['tjr_id'] = $ordersinfo['user_id'];
            $orderdata['bumen'] = $employee['sid'];
            $orderdata['jdtime'] = time();
            $orderdata['qd_depar'] = 6;
            $orderdata['fenlei'] = 83;
            $result = model('order')->save($orderdata);//新建订单表
            if($result){
                $orders = new Orders();
                $order_log = new Order_log();
                $order_id = model('order')->getLastInsID();
                $order_log->log(['id'=>$order_id,'maphone'=>$ordersinfo['tel'],'operation'=>1],$khd);//订单表日志
                Db::name('orders')->update(['id'=>$data['id'],'status'=>1,'order_id'=>$order_id]);//修改中间表
                $sendhuashu->sendMsg(['id'=>$data['id'],'hslx'=>4]);//给转介绍人发微信
                $res = $orders->pushWxToYyjl(['id'=>$order_id,'first'=>'人工审核通过','sendid'=>session('admin_user.adminid')]);
                $info = "<span style='color:green;'>人工</span>审核订单通过！订单进入待分配流程！";
                dellZjsLog(['order_id'=>$data['id'],'info'=>$info,'operation'=>2,'czry'=>session('admin_user.adminid')],$res);//中间表日志
                $this->success('审核成功');
            }else{
                $this->error('审核失败，请重新操作');
            }
        }elseif($data['act'] == 2){//拒绝
            $result = Db::name('orders')->update(['id'=>$data['id'],'status'=>3,'jujueliyou'=>$data['jujueliyou']]);//修改中间表
            if($result){
                $sendhuashu->sendMsg(['id'=>$data['id'],'hslx'=>5,'jujueliyou'=>$data['jujueliyou']]);//给转介绍人发微信
                $info = "<span style='color:red;'>人工</span>审核未通过！";
                dellZjsLog(['order_id'=>$data['id'],'info'=>$info,'operation'=>3,'czry'=>session('admin_user.adminid')]);//中间表日志
                $this->success('拒绝成功');
            }else{
                $this->error('拒绝失败，请重新操作');
            }
        }else{//其他
            $this->error('非法操作');
        }
    }

//    public function doZjsLog($orders_id,$res,$info){
//        $datas['order_id']=$orders_id;
//        $datas['status']=4;
//        if($res['status'] === TRUE){
//            $datas['state'] =1;
//        }else{
//            $datas['state'] =2;
//        }
//        $datas['info'] = $info;
//        addLog($datas);
//    }

    public function editOrders(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $res = Db::name('orders')->update($data);//修改中间表
            if($res){
                dellZjsLog(['order_id'=>$data['id'],'info'=>'修改资料','operation'=>5,'czry'=>session('admin_user.adminid')]);//中间表日志
                return ['msg'=>'修改成功','status'=>1,'url'=>url('index')];
            }else{
                return ['msg'=>'修改失败','status'=>0,'url'=>url('editOrders',['id'=>$data['id']])];
            }
        }else{
            $id = input('id');
            $info = Db::name('orders')->where(['id'=>$id])->find();
            $this->assign('info',$info);
            $user = Db::name('customer')->where(['id'=>$info['user_id']])->find();
            $this->assign('user',$user);
            return $this->fetch('edit');
        }
    }

    public function catlog(){
        $id = input('id');
        $logs = model('zjslog')->where(['order_id'=>$id])->order('id desc')->select();
        $this->assign('list',$logs);
        $this->assign('orderid',$id);
        return $this->fetch();
    }

    public function pushweixin(){
        $sendhuashu = new \app\common\model\SendHuaShua();
        $id = input('id');
        $info = Db::name('orders')->where(['id'=>$id])->find();
        if($info['status'] == 1){
            $res = $sendhuashu->sendMsg(['id'=>$id,'sh'=>'审核成功']);
            if($res['status'] === TRUE){
                return ['msg'=>'发送成功','code'=>1];
            }else{
                return ['msg'=>'发送失败','code'=>2];
            }
        }elseif ($info['status'] == 3){
            $res = $sendhuashu->sendMsg(['id'=>$id,'sh'=>'审核未通过']);
            if($res['status'] === TRUE){
                return ['msg'=>'发送成功','code'=>1];
            }else{
                return ['msg'=>'发送失败','code'=>2];
            }
        }else{
            return ['msg'=>'此订单还未审核，请先审核','code'=>0];
        }
    }

    /**
     * 保存用户
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Employee');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->user_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑用户
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $user = $this->user_model->find($id);
        $depar = Db::name('user')->select();
        return $this->fetch('edit', ['user' => $user,'depars'=>$depar]);
    }

    /**
     * 更新用户
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Employee.edit');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $user           = $this->user_model->find($id);
                $user->id       = $id;
                $user->name = $data['name'];
                $user->phone   = $data['phone'];
                $user->sid    = $data['sid'];
                $user->status   = 1;
                if (!empty($data['password']) && !empty($data['confirm_password'])) {
                    $user->password = md5($data['password'] . Config::get('salt'));
                }
                if ($user->save() !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除用户
     * @param $id
     */
    public function delete($id)
    {
        if ($this->user_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}